package com.airlines.basedata.controller;

import com.airlines.basedata.domain.Fleet;
import com.airlines.basedata.service.FleetService;
import com.airlines.common.Page;
import com.airlines.common.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

/**
 * 描述：机型信息Controller层
 * @author wangkang
 * DATE 2018/9/17
 */
@Controller
@RequestMapping("/fleet")
public class FleetController {
    private static final String PREFIX = "/basedata";
    @Autowired
    private FleetService fleetService;

    /**
     * 进入机型信息页面
     * @return
     */
    @RequestMapping(value = "",method = RequestMethod.GET)
    public String toFleetPage(){
        return PREFIX + "/fleetList";
    }

    /**
     * 多条件模糊查询，带分页
     * @param fleet
     * @param offset
     * @param limit
     * @return
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Page list(Fleet fleet,String offset, String limit){
        Page page = new Page();
        page.setPageNumber((Integer.valueOf(offset) / (Integer.valueOf(limit))));
        page.setPageSize(Integer.valueOf(limit));
        page = fleetService.listData(fleet,page);
        System.out.println("-------------------------------------------"+fleet.toString());
        return page;
    }

    /**
     * 进入新增页面
     * @return
     */
    @RequestMapping(value = "/add")
    public String add(){
        return PREFIX + "/fleetAdd";
    }

    /**
     * 保存新增
     * @param fleet
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/save",method = RequestMethod.POST)
    public R save(Fleet fleet){
        fleet.setOpTime(new Date());
        int row = fleetService.save(fleet);
        if(row>0){
            System.out.println("新增的数据为"+row+"条---------------------------");
            return R.ok();
        }
        return R.error();
    }

    /**
     * 根据机型删除
     * @param acType
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    public R delete(String acType){
        int row = fleetService.deleteByPrimaryKey(acType);
        if(row>0){
            return R.ok();
        }
        return R.error();
    }
}
